package es.buffygilmore.collectionapp;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class CollectionApp extends ListActivity {

	private static final int ACTIVITY_CREATE_COLLECTION = 0;
	private static final int ACTIVITY_ACCESS_ELEMENTS = 2;

	private CollectionAppDbAdapter mDbHelper;

	private ImageView addButton;

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.listado_colecciones);
		this.addButton = (ImageView) this.findViewById(R.id.botonAdd);
		this.mDbHelper = new CollectionAppDbAdapter(this);
		this.mDbHelper.open();
		this.fillColeccionesData();
		this.registerForContextMenu(this.getListView());

		this.addButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(final View view) {
				CollectionApp.this.addButton.setColorFilter(0xEEBD8ED7, PorterDuff.Mode.MULTIPLY);
				CollectionApp.this.crearColeccion();
			}

		});
	}

	private void fillColeccionesData() {
		// Get all of the rows from the database and create the item list
		final Cursor coleccionesCursor = this.mDbHelper.obtenerTodasColecciones();
		this.startManagingCursor(coleccionesCursor);

		// Create an array to specify the fields we want to display in the list (only TITLE)
		final String[] from = new String[] { CollectionAppDbAdapter.KEY_COLECCION_TITULO,
				CollectionAppDbAdapter.KEY_COLECCION_DESCRIPCION };

		// and an array of the fields we want to bind those fields to (in this case just text1)
		final int[] to = new int[] { R.id.coleccion1, R.id.coleccion2 };

		// Now create a simple cursor adapter and set it to display
		final SimpleCursorAdapter colecciones = new SimpleCursorAdapter(this,
				R.layout.fila_coleccion, coleccionesCursor, from, to);
		this.setListAdapter(colecciones);
	}

	private void crearColeccion() {
		final Intent i = new Intent(this, ColeccionEdit.class);
		this.startActivityForResult(i, ACTIVITY_CREATE_COLLECTION);
	}

	@Override
	protected void onListItemClick(final ListView l, final View v, final int position, final long id) {
		super.onListItemClick(l, v, position, id);
		final Intent i = new Intent(this, ElementoList.class);
		i.putExtra(CollectionAppDbAdapter.KEY_COLECCION_ROWID, id);
		// Se recupera la coleccion y se pasa
		final Cursor coleccionSeleccionada = this.mDbHelper.obtenerColeccion(id);
		i.putExtra(CollectionAppDbAdapter.KEY_COLECCION_TITULO, coleccionSeleccionada
				.getString(coleccionSeleccionada
						.getColumnIndexOrThrow(CollectionAppDbAdapter.KEY_COLECCION_TITULO)));
		this.startActivityForResult(i, ACTIVITY_ACCESS_ELEMENTS);
	}

	@Override
	protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		this.addButton.clearColorFilter();
		this.fillColeccionesData();
	}

}
